package com.android.mybaidu;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;

public class Main extends MapActivity {
	// 添加百度地图的相关控件
	private MapView mapView;
	private BMapManager bMapManager;// 加载地图的引擎
	// 百度地图的key
	private String keyString = "A270F85CD72A01E8519A9677A75FB4016ED9A5A3";
	// 在百度地图上添加一些控件，比如是放大或者缩小的控件
	private MapController mapController;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapView = (MapView) this.findViewById(R.id.bmapView);
		bMapManager = new BMapManager(Main.this);
		// 必须要加载key
		bMapManager.init(keyString, new MKGeneralListener() {
			@Override
			public void onGetPermissionState(int arg0) {
				if (arg0 == 300) {
					Toast.makeText(Main.this, "输入的Key有错！请核实！！", 1).show();
				}
			}

			@Override
			public void onGetNetworkState(int arg0) {
			}
		});

		this.initMapActivity(bMapManager);
		mapView.setBuiltInZoomControls(true);// 表示可以设置缩放功能
		mapController = mapView.getController();
		mapController.setZoom(12);// 设置缩放级别是12个级别

		Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);

		mapView.getOverlays().add(new MyOverLayItem(drawable));
	}

	public class MyOverLayItem extends ItemizedOverlay<OverlayItem> {

		private List<OverlayItem> list = new ArrayList<OverlayItem>();
		// 定义一组坐标，都是一个double类型定义

		private double mLat1 = 39.90923;// 表示纬度
		private double mLon1 = 116.397428;// 表示经度

		private double mLat2 = 39.9022;// 表示纬度
		private double mLon2 = 116.3922;// 表示经度

		private double mLat3 = 39.917723;// 表示纬度
		private double mLon3 = 116.37222;// 表示经度

		// 用于在地图上标识坐标,用一个图片标注
		public MyOverLayItem(Drawable drawable) {
			super(drawable);
			// TODO Auto-generated constructor stub
			// 第一组数据的在地图上的标注点
			GeoPoint geoPoint1 = new GeoPoint((int) (mLat1 * 1E6),
					(int) (mLon1 * 1E6));
			GeoPoint geoPoint2 = new GeoPoint((int) (mLat2 * 1E6),
					(int) (mLon2 * 1E6));
			GeoPoint geoPoint3 = new GeoPoint((int) (mLat3 * 1E6),
					(int) (mLon3 * 1E6));
			list.add(new OverlayItem(geoPoint1, "Point1", "Point1"));
			list.add(new OverlayItem(geoPoint2, "Point2", "Point2"));
			list.add(new OverlayItem(geoPoint3, "Point3", "Point3"));
			populate();// 刷新地图的功能
		}

		// 返回指定的list集合中每一个坐标
		@Override
		protected OverlayItem createItem(int arg0) {
			return list.get(arg0);
		}
		@Override
		public int size() {
			return list.size();
		}
		@Override
		public boolean onTap(int i) {
			Toast.makeText(Main.this, list.get(i).getSnippet(), 1).show();
			return true;
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (bMapManager != null) {
			bMapManager.destroy();
			bMapManager = null;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (bMapManager != null) {
			bMapManager.start();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (bMapManager != null) {
			bMapManager.stop();
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}